package cn.lysy.xiaoruan.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.oragee.banners.BannerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import cn.lysy.xiaoruan.R;
import cn.lysy.xiaoruan.adapter.GoodsListAdapter;
import cn.lysy.xiaoruan.base.BaseFragment;
import cn.lysy.xiaoruan.javabean.Goodslist;
import cn.lysy.xiaoruan.model.GoodsModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment {

    private ImageButton code;
    private String[] imgs = { "a.jpg" , "b.jpg" , "c.jpg" , "d.jpg" , "e.jpg" , "f.jpg" };
    private List<View> viewList;
    private BannerView bannerView;
    private RecyclerView ershou;
    private RecyclerView remai;
    private Call call;



    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        code = view.findViewById(R.id.code);
        code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentIntegrator integrator = new IntentIntegrator(getActivity());
                integrator.initiateScan();

                // 设置要扫描的条码类型，ONE_D_CODE_TYPES：一维码，QR_CODE_TYPES-二维码
                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                integrator.setPrompt("请扫描二维码"); //底部的提示文字，设为""可以置空
                integrator.setCameraId(1); //前置或者后置摄像头
                integrator.setBeepEnabled(false); //扫描成功的「哔哔」声，默认开启
                integrator.setBarcodeImageEnabled(true);//是否保留扫码成功时候的截图
                integrator.initiateScan();
            }
        });
        viewList=new ArrayList<>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getActivity());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //image.setImageResource(imgs[i]);
            Picasso.with(this.getActivity())
                    .load("http://120.78.143.69/xiaoruan/Public/Upload/ad/"+imgs[i])
                    .into(image);
            viewList.add(image);
        }

        bannerView=view.findViewById(R.id.banner);
        bannerView.setViewList(viewList);
        bannerView.startLoop(true);

        remai=view.findViewById(R.id.huobaoremai);
        ershou=view.findViewById(R.id.jingxuanershou);
        GoodsModel goodsModel=new GoodsModel();
        call=goodsModel.Goodlist(1);
        Callback<List<Goodslist>> callback=new Callback<List<Goodslist>>() {
            @Override
            public void onResponse(Call<List<Goodslist>> call, Response<List<Goodslist>> response) {
                List<Goodslist> goodslists = response.body();
                if (getActivity()!=null){
                    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
                    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    GoodsListAdapter adapter=new GoodsListAdapter(getActivity(),goodslists);
                    ershou.setLayoutManager(linearLayoutManager);
                    ershou.setAdapter(adapter);
                    ershou.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<Goodslist>> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (scanResult != null) {
            String result = scanResult.getContents();
            Log.e("HYN", result);
            Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);


    }
}